using System;
using System.Collections;
using System.Linq;
using EzPos.DataAccess.Table;
using EzPos.Properties;

namespace EzPos.Service.Table
{
    public class TableService
    {
        private readonly TableDataAccess _tableDataAccess;

        public TableService(TableDataAccess tableDataAccess)
        {
            _tableDataAccess = tableDataAccess;
        }

        public virtual IList GetTables()
        {
            return _tableDataAccess.GetTables();
        }

        public virtual IList GetTables(int id)
        {
            return _tableDataAccess.GetTables(id);
        }

        public virtual IList GetTables(String name)
        {
            return _tableDataAccess.GetTables(name);
        }

        public virtual IList GetTables(IList searchCriteria)
        {
            if (searchCriteria == null)
                throw new ArgumentNullException("searchCriteria", Resources.MsgInvalidSearchCriteria);

            return _tableDataAccess.GetTables(searchCriteria);
        }

        public virtual void TableManagement(Model.Table.Table table, string requestCode)
        {
            if (requestCode == null)
                throw new ArgumentNullException("requestCode", Resources.MsgInvalidRequest);

            if (table == null)
                throw new ArgumentNullException("table", Resources.MsgInvalidTable);

            if (requestCode == Resources.OperationRequestInsert)
                _tableDataAccess.InsertTable(table);
            else if (requestCode == Resources.OperationRequestDuplicate)
            {
                table.TableId = 0;
                _tableDataAccess.InsertTable(table);
            }
            else if (requestCode == Resources.OperationRequestUpdate)
                _tableDataAccess.UpdateTable(table);
            else
            {
                _tableDataAccess.DeleteTable(table);
            }           
        }

        public Model.Table.Table RetrieveTable(IList tableList, string tableName)
        {
            return
                tableList.Cast<Model.Table.Table>().
                Where(table => table != null).
                FirstOrDefault(table => table.TableName.ToLower().Equals(tableName.ToLower()));
        }

        public string TableRelocationHandler(IList tableList, string fromTableName, string toTableName)
        {
            var resultString = string.Empty;

            if(fromTableName.Equals(toTableName))
                return Resources.MsgInvalidTableMovement;

            if (string.IsNullOrEmpty(fromTableName))
                return Resources.MsgInvalidTableMovement;

            if(string.IsNullOrEmpty(toTableName))
                return Resources.MsgInvalidTableMovement;

            var fromTable = RetrieveTable(tableList, fromTableName);
            if (fromTable == null)
                return Resources.MsgInvalidTableMovement;

            if (fromTable.SaleOrderId == 0)
                return Resources.MsgInvalidTableMovement;

            var toTable = RetrieveTable(tableList, toTableName);
            if ((toTable == null) || (toTable.SaleOrderId != 0))
                return Resources.MsgInvalidTableMovement;

            //Move to destination table
            toTable.SaleOrderId = fromTable.SaleOrderId;
            toTable.StatusId = fromTable.StatusId;
            TableManagement(toTable, Resources.OperationRequestUpdate);

            //Clear expeditor table
            fromTable.SaleOrderId = 0;
            fromTable.StatusId = 0;
            TableManagement(fromTable, Resources.OperationRequestUpdate);

            return resultString;
        }
    }
}